﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Kiiro.SiteDefinition.ControlTemplates
{
    public partial class CustomField : System.Web.UI.UserControl
    {
        private string fieldName;
        private string requiredText;
        private string helpText;
        private BaseFieldControl baseFieldControl;

        public string FieldName
        {
            get { return fieldName; }
            set { fieldName = value; }
        }

        public string RequiredText
        {
            get { return requiredText; }
            set { requiredText = value; }
        }

        public string HelpText
        {
            get { return helpText; }
            set { helpText = value; }
        }

        public BaseFieldControl BaseFieldControl
        {
            get { return baseFieldControl; }
            set { baseFieldControl = value; }
        }

        public void AddBaseFieldControl ( BaseFieldControl baseFieldControl )
        {
            BaseFieldControl = baseFieldControl;
            Controls.Add ( baseFieldControl );
        }

        public string ValidateCustomField ( )
        {
            string errorMessage = default ( string );

            if ( !BaseFieldControl.IsValid )
            {
                errorMessage = BaseFieldControl.ErrorMessage;
            }

            return errorMessage;
        }

        public void SetVisible ( SPContext context )
        {
            if ( context != null && context.FormContext != null && context.FormContext.FormMode == SPControlMode.Display &&
                BaseFieldControl.ListItemFieldValue == null )
            {
                Visible = false;
            }
        }

        public override void RenderControl ( HtmlTextWriter writer )
        {            
            if ( BaseFieldControl.ControlMode == SPControlMode.Display )
            {
                if ( Visible )
                {
                    writer.Write ( string.Format ( @"<div class='row'>
                        <dl>
                            <dt><label>{0}</label></dt>
                            <dd class='input'>", fieldName ) );

                    RenderChildren ( writer );

                    writer.Write ( @"</dd>
                            </dl>
                        </div>");
                }
            }
            else
            {
                writer.Write ( string.Format ( @"<div class='row'>
                        <dl>
                            <dt><label>{0}</label> {1}</dt>
                            <dd class='input'>", fieldName, requiredText ) );

                RenderChildren ( writer );

                writer.Write ( string.Format ( @"</dd>
                        </dl>
                        <dl>
                            <dd>{0}</dd>
                        </dl>
                    </div>", helpText ) );
            }
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/CONTROLTEMPLATES/Kiiro/CustomField.ascx.cs $
 * 
 * 4     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/